package zj.it.bhne.gridengineeringsurvey.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;

import java.util.List;

import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Photos;
import zj.it.bhne.gridengineeringsurvey.myutils.MyPath;
import zj.it.bhne.gridengineeringsurvey.myutils.MyUtils;
import zj.it.bhne.gridengineeringsurvey.myutils.XCRoundRectImageView;

import static zj.it.bhne.gridengineeringsurvey.fragment.OneFragment.deleteDir;

/**
 * EquActivity中的显示视频图片列表的适配器
 * */
public class MultAdapter extends RecyclerView.Adapter<MultAdapter.UserHolder> {

    private Context context;
    private List<Photos> list;

    public MultAdapter(Context context, List<Photos> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return new UserHolder(inflater, parent);

    }


    @Override
    public void onBindViewHolder(@NonNull UserHolder holder, int position) {
        final Photos user = list.get(position);
        if (user.getPath().contains(".mp4")) {


            try {
                MediaMetadataRetriever media = new MediaMetadataRetriever();
                media.setDataSource(user.getPath());
                Bitmap bitmap = media.getFrameAtTime();
                holder.imageView.setImageBitmap(MyUtils.zoomImg(bitmap, 120, 120));
            } catch (Exception e) {
                e.printStackTrace();
            }
            holder.name.setText("视频");
        } else if (user.getPath().contains(".jpg")) {

            try {
                String path = user.getPath();
                Bitmap bmpDefaultPic = BitmapFactory.decodeFile(path);
                holder.imageView.setImageBitmap(MyUtils.zoomImg(bmpDefaultPic, 120, 120));

            } catch (Exception e) {
                e.printStackTrace();
            }
            //holder.imageView.setBackgroundResource(R.drawable.camera);
            holder.name.setText("照片");
        }


        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operationDialog(context,user.getPath());
            }
        });

    }

    /**
     * 点击多媒体进行的操作
     * */
    private void operationDialog(final Context context, final String path) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.content("请选择需要进行的点操作")//
                .style(NormalDialog.STYLE_TWO)//
                .btnNum(3)
                .title("多媒体操作")
                .btnText("取消", "打开" , "删除")//
                .show();
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        MyPath.openFileByPath(context, path);
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        if (deleteDir(path))
                            MyUtils.getToast(context,"删除成功");
                    }
                });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class UserHolder extends RecyclerView.ViewHolder {//用于托管每一个用于显示的单个项目，注意它继承自RecyclerView.ViewHolder
        private XCRoundRectImageView imageView;
        private TextView name;
        private LinearLayout button;

        private UserHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.activity_equ_mult, parent, false));//每一个项目在显示的时候都只是显示R.layout.list_item里面的按钮但实际上每一个按钮既有按钮，又有用户信息

            imageView = itemView.findViewById(R.id.mul_image);//Button这个按钮实例
            name = itemView.findViewById(R.id.mul_name);
            button = itemView.findViewById(R.id.mul_button);
        }
    }


}
